package com.zyd.shiro.business.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.zyd.shiro.business.entity.Categories;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.Channel;
import com.zyd.shiro.persistence.dao.MallCategorieDao;
import com.zyd.shiro.persistence.dao.MallCategoryDao;
import com.zyd.shiro.persistence.dao.SysChannelDao;
import com.zyd.shiro.util.QiniuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 服务层
 */
@Service
public class MallChannelService {
    @Autowired
    private SysChannelDao sysChannelDao;

    public List<Channel> findAll() {

        return sysChannelDao.findAll();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    public Page<Channel> findPage(int page, int size) {
        PageRequest pageRequest = new PageRequest(page-1, size);
        return (Page<Channel>) sysChannelDao.findAll(pageRequest);
    }

    private Specification<Channel> where(Map searchMap) {

        return new Specification<Channel>() {

            @Override
            public Predicate toPredicate(Root<Channel> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                //
                if (searchMap.get("app_name")!=null && !"".equals(searchMap.get("app_name"))) {
                    predicateList.add(cb.like(root.get("app_name").as(String.class), "%"+(String)searchMap.get("app_name")+"%"));
                }
                //
                if (searchMap.get("app_id")!=null && !"".equals(searchMap.get("app_id"))) {
                    predicateList.add(cb.like(root.get("app_id").as(String.class), "%"+(String)searchMap.get("app_id")+"%"));
                }
                //
                if (searchMap.get("app_secret")!=null && !"".equals(searchMap.get("app_secret"))) {
                    predicateList.add(cb.like(root.get("app_secret").as(String.class), "%"+(String)searchMap.get("app_secret")+"%"));
                }
                //
                if (searchMap.get("phone_no")!=null && !"".equals(searchMap.get("phone_no"))) {
                    predicateList.add(cb.like(root.get("phone_no").as(String.class), "%"+(String)searchMap.get("phone_no")+"%"));
                }
                //
                if (searchMap.get("mchid")!=null && !"".equals(searchMap.get("mchid"))) {
                    predicateList.add(cb.like(root.get("mchid").as(String.class), "%"+(String)searchMap.get("mchid")+"%"));
                }
                //
                if (searchMap.get("apikey")!=null && !"".equals(searchMap.get("apikey"))) {
                    predicateList.add(cb.like(root.get("apikey").as(String.class), "%"+(String)searchMap.get("apikey")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

            }
        };

    }

    public Page<Channel> findSearch(Map whereMap, int page, int size) {
        Specification<Channel> specification = where(whereMap);
        PageRequest pageRequest = new PageRequest(page-1, size);
        return (Page<Channel>) sysChannelDao.findAll(specification, pageRequest);
    }

    public Channel findOne(Integer id) {
        return sysChannelDao.findOne(id);
    }

    public void add(Channel wxapp) {

        sysChannelDao.save(wxapp);
    }

    public void update(Channel wxapp) {
        sysChannelDao.save(wxapp);
    }

    public void delete(Integer id) {
        sysChannelDao.delete(id);
    }

    public void deleteList(Integer[] ids) {
        for (Integer id : ids) {
            sysChannelDao.delete(id);
        }
    }

    public Channel getChannelByUserId(Integer uid)
    {
        return sysChannelDao.getChannelByUserId(uid);
    }


}
